/////////////////////////////////////////////////////////////////////////////
// 2023 -
//open source under Apache License Version 2.0
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once


#include <map>
#include <vector>
#include <shared_mutex>
#include <memory>
#include <kmhdfs/client/ec_policy.h>
#include <turbo/base/macros.h>

enum PolicyId {
    REPLICATION = 0,
    RS_6_3 = 1,
    RS_3_2 = 2,
    RS_6_3_LEGACY = 3,
    XOR_2_1 = 4,
    RS_10_4 = 5
};

namespace Hdfs {
    namespace Internal {
        class SystemECPolicies {
        public:
            static SystemECPolicies &getInstance() {
                static SystemECPolicies instance;
                return instance;
            }

            /**
             * get system ec policy by id.
             * @param id The ec policy id.
             */
            std::shared_ptr<ECPolicy> getById(int8_t id);

            /**
             * add a new ec policy.
             * @param id The ec policy id.
             * @param ecPolicy The new ec policy.
             */
            void addEcPolicy(int8_t id, std::shared_ptr<ECPolicy> ecPolicy);

        private:
            TURBO_DISALLOW_COPY_AND_ASSIGN(SystemECPolicies);

            SystemECPolicies();

            ~SystemECPolicies() = default;

        private:
            static std::shared_mutex mutex;

            // 1MB
            const int32_t cellsize = 1024 * 1024;

            std::vector<std::shared_ptr<ECPolicy> > sysPolicies;
            std::map<int8_t, std::shared_ptr<ECPolicy> > maps;
        };
    }
}
